/*----------------------------------------------------------------------------------	
 //声明任务栏空间，任务栏相关js操作
 ----------------------------------------------------------------------------------*/
desktop.taskbar = {
	timer : function(obj) {
		var curDaytime = new Date().toLocaleString().split(" ");
		obj.innerHTML = curDaytime[1];
		obj.title = curDaytime[0];
		setInterval(function() {
					obj.innerHTML = new Date().toLocaleString().split(" ")[1];
				}, 1000);
	},
	upTaskWidth : function() {
		var taskbar = desktop.taskbar.element;
		var maxHdTabNum = Math.floor($(window).width() / 100);
		// 计算任务栏宽度
		taskbar.width(maxHdTabNum * 100);
		// 存储活动任务栏tab默认组数
		$('body').data("maxHdTabNum", maxHdTabNum - 2);
	},
	init : function() {
		// 读取元素对象数据
		var me = this;
		me.upTaskWidth();
		// 当改变浏览器窗口大小时，重新计算任务栏宽度
		$(window).wresize(function() {
					me.upTaskWidth();
				});

	},
	contextMenu : function(tab, id) {
		// 初始化任务栏Tab右键菜单
		var data = [[{
			text : "最大化",
			func : function() {
				$("#myWin_" + tab.data('win')).find('a.winMaximize')
						.trigger('click');
			}
		}, {
			text : "最小化",
			func : function() {
				desktop.win.minimize($("#myWin_" + tab.data('win')));
			}
		}], [{
					text : "关闭",
					func : function() {
						$("#smartMenu_taskTab_menu" + id).remove();
						desktop.win.close($("#myWin_" + tab.data('win')));
					}
				}]];
		desktop.contextMenu(tab, data, "taskTab_menu" + id, 10);
	},
	addWinTab : function(text, id) {
		var myData = desktop.getMydata();
		var tasklb = desktop.taskbar.tasklb, tid = "myWinTab_" + id, allTab = tasklb
				.find('a'), curTabNum = allTab.size(), docHtml = "<a href='#' id='"
				+ tid + "'>" + text + "</a>";

		// 添加新的tab
		tasklb.append($(docHtml));
		var $newTab = $("#" + tid);
		// 右键菜单
		this.contextMenu($newTab, id);

		tasklb.find('a.selectTab').removeClass('selectTab')
				.addClass('defaultTab');

		$newTab.data('win', id).addClass('selectTab').click(function() {
					var win = $("#myWin_" + $(this).data('win'));

					if (win.is(":hidden")) {
						win.show();
						$(this).removeClass('defaultTab').addClass('selectTab'); // 当只有一个窗口时
						desktop.win.switchZindex(win);
					} else {
						if ($(this).hasClass('selectTab')) {
							desktop.win.minimize(win);
						} else {
							desktop.win.switchZindex(win);
						}
					}
				});

		$('body').data("topWinTab", $newTab);

		// 当任务栏活动窗口数超出时
		if (curTabNum > myData.maxHdTabNum - 1) {
			var LeftBtn = $('#leftBtn'), rightBtn = $('#rightBtn');

			LeftBtn.show().find("a").click(function() {
						var pos = tasklb.position();
						if (pos.top < 0) {
							tasklb.animate({
										"top" : pos.top + 40
									}, 50);
						}
					});

			rightBtn.show().find("a").click(function() {
						var pos = tasklb.position(), h = tasklb.height(), row = h
								/ 40;
						if (pos.top > (row - 1) * (-40)) {
							tasklb.animate({
										"top" : pos.top - 40
									}, 50);
						}
					});

			tasklb.parent().css("margin", "0 100");
		}

	},
	delWinTab : function(wObj) {
		var tasklb = desktop.taskbar.tasklb, LeftBtn = $('#leftBtn'), rightBtn = $('#rightBtn'), pos = tasklb
				.position();

		this.findWinTab(wObj).remove();

		var newH = tasklb.height();
		if (Math.abs(pos.top) == newH) {
			LeftBtn.find('a').trigger("click");
		}
		if (newH == 40) {
			LeftBtn.hide();
			rightBtn.hide();
			tasklb.parent().css("margin", 0);
		}
	},
	findWinTab : function(wObj) {
		var tasklb = desktop.taskbar.tasklb, objTab;
		tasklb.find('a').each(function(index) {
					var id = "#myWin_" + $(this).data("win");
					if ($(id).is(wObj)) {
						objTab = $(this);
					}
				});
		return objTab;
	}
};